package com.oig.sys.workflow.controller;

import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupChange;
import com.oig.sys.workflow.app.IAppFlowInstanceService;
import com.oig.sys.workflow.dto.ApprovalAgentReqDto;
import com.oig.sys.workflow.vo.InstanceAllInfoVo;
import com.oig.sys.workflow.vo.ApprovalAgentRsp;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 创建审批
 */
//@Hidden
@RestController
@RequestMapping("/flow")
@Slf4j
@RequiredArgsConstructor
public class TbFlowInstanceController {

    private final IAppFlowInstanceService appFlowInstanceService;

    /**
     * 未提交成功的重新提交，可以另外加个线程轮询 重新提交
     */
    //@SysLog(value="创建审批流",apiUrl="/flow/apply")
    @Operation(summary = "创建审批流")
    @PostMapping(value = "/apply")
    public ApprovalAgentRsp apply(@RequestBody @Validated({GroupAdd.class}) ApprovalAgentReqDto approvalAgentReqDto){
        log.info("app instance :{}", approvalAgentReqDto);
        return this.appFlowInstanceService.apply(approvalAgentReqDto);
    }

    /**
     * 未提交成功的重新提交，可以另外加个线程轮询 重新提交
     */
    //@SysLog(value="撤销审批流",apiUrl="/flow/cancel")
    @Operation(summary = "撤销审批流")
    @PostMapping(value = "/cancel")
    public SysResponse<String> cancel(@RequestBody @Validated({GroupChange.class}) ApprovalAgentReqDto approvalAgentReqDto){
        log.info("cancel instance :{}", approvalAgentReqDto);
        String msg = this.appFlowInstanceService.cancel(approvalAgentReqDto);
        return SysResponse.ok(msg);
    }

    @Operation(summary = "撤销审批流")
    @PostMapping(value = "/getDetail")
    public InstanceAllInfoVo getDetail(@RequestBody @Validated({GroupChange.class}) ApprovalAgentReqDto approvalAgentReqDto){
        log.info("get instance :{}", approvalAgentReqDto);
        return this.appFlowInstanceService.getDetail(approvalAgentReqDto);
    }

    @Operation(summary = "单个触发通知")
    @PostMapping(value = "/notify/{id}")
    public SysResponse<String> notify(@PathVariable("id") Integer id){
        log.debug("do notify {}",id);
        this.appFlowInstanceService.notifyInstanceStatus(id);
        return SysResponse.ok();
    }


}
